package com.nuoniu.sibanyun.mapper.erp.in;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.erp.in.InUnit;
import com.nuoniu.sibanyun.entity.erp.in.InUnitGroup;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 计量单位 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@Component
@Mapper
public interface InUnitMapper extends BaseMapper<InUnit> {

    /**
     * 根据计量组获取计量单位
     * @author 小啊刚
     * @date 2020-09-25
     */
    @Select("select * from scb_unit where unit_group_id = #{groupId}")
    List<InUnit> getByGroup(Integer groupId);

    @Select("select * from scb_unit where name=#{name} and unit_group_id=0 and company_id=#{companyId}")
    InUnit getByName(String name, Integer companyId);

    @Delete("delete from scb_unit where unit_group_id=#{unitGroupId}")
    void getRemoveByGroupId(Integer unitGroupId);

    /**
     * 获取主计量单位
     * @author 小啊刚
     * @date 2021-01-22
     */
    @Select("select * from scb_unit where unit_group_id=#{groupId} and is_main_unit=1")
    InUnit getMainUnit(Long groupId);

    @Select("select * from scb_unit where code=#{code} and unit_group_id=0 and company_id=#{companyId}")
    InUnit getByCode(String code, Integer companyId);

    /**
     * 获取单计量名称列表
     * @author 小啊刚
     * @date 2021-07-16
     */
    @Select("select name from scb_unit where company_id=#{companyId} and unit_group_id=0")
    List<String> getNameList(Integer companyId);

    /**
     * 获取计量分组名称列表
     * @author 小啊刚
     * @date 2021-11-29
     */
    @Select("select name from scb_unit_group where company_id=#{companyId}")
    List<String> getGroupNameList(Integer companyId);

    /**
     * 获取计量组
     * @author 小啊刚
     * @date 2021-11-29
     */
    @Select("select * from scb_unit_group where name=#{unitGroupName} and company_id=#{companyId}")
    InUnitGroup getGroupByName(String unitGroupName, Integer companyId);

    /**
     * 随机获取一个辅助计量
     * @author 小啊刚
     * @date 2021-11-29
     */
    @Select("select * from scb_unit where unit_group_id=#{groupId} and is_main_unit=0 limit 1")
    InUnit getUnitByGroup(int groupId);
}
